package com.dailyblue.javaee.controller;

import com.dailyblue.javaee.bean.Emp;
import com.dailyblue.javaee.service.EmpService;
import com.dailyblue.javaee.service.impl.EmpServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/emp_find_page")
public class EmpFindPageController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 1. 获取前端页面的参数
        int page ;
        String p = request.getParameter("page");
        if(p==null){
            page = 1;
        }else{
            page = Integer.parseInt(p);
        }
        // 2. 产生对象
        EmpService service = new EmpServiceImpl();
        // 3. 调用方法 分别获取数据和页码数
        List<Emp> list = service.find(page);
        int pageNum = service.findAllPageNum();
        // 4. 获取session并存放到容器
        HttpSession session = request.getSession();
        session.setAttribute("LIST",list);
        session.setAttribute("PAGENUM",pageNum);
        // 5. 重定向到页面
        response.sendRedirect("emp.jsp");
    }
}